package com.atguigu.gmall.cart.service.impl;

import com.atguigu.gmall.cart.mapper.CartInfoMapper;
import com.atguigu.gmall.cart.service.CartInfoAsyncService;
import com.atguigu.gmall.model.cart.CartInfo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class CartInfoAsyncServiceImpl implements CartInfoAsyncService {

    @Resource
    private CartInfoMapper cartInfoMapper;

    @Async
    @Override
    public void insert(CartInfo cartInfo) {
        cartInfoMapper.insert(cartInfo);
    }

    @Async
    @Override
    public void updateByIdAsyncId(String userTempId, String userId) {
        CartInfo cartInfo = new CartInfo();
        cartInfo.setUserId(userId);
        cartInfoMapper.update(cartInfo, new QueryWrapper<CartInfo>()
                .eq("user_id", userTempId));
    }

    @Async
    @Override
    public void update(CartInfo cartInfo) {
        cartInfoMapper.update(cartInfo, new QueryWrapper<CartInfo>()
                .eq("user_id", cartInfo.getUserId())
                .eq("sku_id", cartInfo.getSkuId()));
    }

    @Override
    public void delete(String userTempId) {
        cartInfoMapper.delete(new QueryWrapper<CartInfo>()
        .eq("user_id",userTempId));
    }
}
